package com.mt.sdk.longdatas.protocol;

import com.mt.sdk.tools.MTTools;

public class TransferprotocolHelp {

	private final static byte START_BYTE = (byte) (0xAA & 0xFF);
	private final static byte MID_BYTE = (byte) (0xA5 & 0xFF);
	private final static byte END_BYTE = (byte) (0x55 & 0xFF);

	private int recive_buffer_end = 0;
	private static final int BUFFER_SIZE = 1024;
	private byte[] recive_buffer = new byte[BUFFER_SIZE];

	private TransferprotocolHelpCallBack TransferprotocolHelpCallBack;

	// 设置回调
	public void setCallback(TransferprotocolHelpCallBack callbac) {
		TransferprotocolHelpCallBack = callbac;
	}
	public void putDatas(byte[] datas) {
		System.out.println("进入解码数据->"+MTTools.convertBytearrayToString(datas));
		for (byte b : datas) {
			putByte(b);
		}
	}

	// 获取一个字节数据
	private boolean hasmid = false;
	private boolean hasstart = false;
	public void putByte(byte data) {

		if (hasmid) {
			recive_buffer[recive_buffer_end] = data;
			recive_buffer_end = (recive_buffer_end + 1) % recive_buffer.length;
			hasmid = false;
		} else {
			if (data == START_BYTE) { // 遇到起始位置
				hasstart = true;
				recive_buffer_end = 0;
				return;
			}

			if (!hasstart) { // 如果没有遇到起始位置，就直接丢弃
				return;
			}

			if (data == MID_BYTE) { // 遇到转义
				hasmid = true;
				return;
			}

			if (data == END_BYTE) { // 遇到结束，进行回调反馈
				hasstart = false;
				hasmid = false;
				byte[] full_byte = new byte[recive_buffer_end];
				for(int i=0; i<recive_buffer_end; i++){
					full_byte[i] = recive_buffer[i];
				}
				if (TransferprotocolHelpCallBack != null) {
					TransferprotocolHelpCallBack.onGetcmd(full_byte);
				}
				return;
			}
			recive_buffer[recive_buffer_end] = data; // 直接进行存储
			recive_buffer_end = (recive_buffer_end + 1) % recive_buffer.length;
		}
	}

	// 回调命令
	public static interface TransferprotocolHelpCallBack {
		public void onGetcmd(byte[] cmd);
	}

	// 数据打包成发送的形式
	public static byte[] packgeDatas(byte[] datas) {

		if ((datas == null) || (datas.length < 1)) {
			return null;
		}
//		System.out.println("实际长度->"+datas.length);
		
		int lenght = 0;
		for (int i = 0; i < datas.length; i++) {
			if ((START_BYTE == datas[i]) || (MID_BYTE == datas[i])
					|| (END_BYTE == datas[i])) {
				lenght++;
			}
		}
		lenght += datas.length;
		lenght += 2; // 添加起始位和结束位

		byte[] data = new byte[lenght];

		int j = 0;
		data[j++] = START_BYTE;
		for (int i = 0; i < datas.length; i++) {
			if ((START_BYTE == datas[i]) || (MID_BYTE == datas[i])
					|| (END_BYTE == datas[i])) {
				data[j++] = MID_BYTE; // 添加转义
			}
			data[j++] = datas[i];
		}
		data[j++] = END_BYTE;

		return data;
	}

	// 检查校验
	public static boolean checkErro(byte[] datas) {

		if (datas == null) {
			return false;
		}

		byte lrc = 0;
		for (int i = 0; i < datas.length; i++) { // 计算校验
			lrc += datas[i];
		}

		if (lrc == 0) { // 校验没问题
			return true;
		}

		return false;
	}

	// 获取校验值
	public static byte getLRCCheckByte(byte[] datas, int lenght) {
		byte lrc = 0;
		int lenght_tmp = lenght;
		if(lenght > datas.length){
			lenght_tmp = datas.length;
		}
		for (int i = 0; i < lenght_tmp; i++) {
			lrc += datas[i];
		}
		lrc = ((byte) -lrc);

		return lrc;
	}
}
